package cn.wolfcode.crm.service.impl;


import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.mapper.RoleMapper;
import cn.wolfcode.crm.query.QueryForObject;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private RoleMapper RoleMapper;

    @Override
    public void deleteByPrimaryKey(Long id) {
        //删除关系
        employeeMapper.deleteRelation(id);
        employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insert(Employee record) {
        //初始化密码
        record.setPassword(new Md5Hash("1", record.getUsername(), 3).toString());
        //设置初始化状态
        record.setState(Employee.STATE_NORMAL);
        employeeMapper.insert(record);

        List<Role> roles = record.getRoles();
        if (roles != null) {
            for (Role role : roles) {
                RoleMapper.insertRoleRelation(role.getId(),record.getId());
            }
        }
        employeeMapper.deleteRelation(record.getId());
    }

    @Override
    public Employee selectByPrimaryKey(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> selectAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public void updateByPrimaryKey(Employee record) {
        //删除关系
        employeeMapper.deleteRelation(record.getId());
        List<Role> roles = record.getRoles();
        if (roles != null) {
            for (Role role : roles) {
                //更新关系
                RoleMapper.insertRoleRelation( role.getId(),record.getId());
            }
        }
        employeeMapper.updateByPrimaryKey(record);
    }

    public PageResult query(QueryForObject qo) {
        //1:查总条数
        int total = employeeMapper.queryForCount(qo);

        if (total == 0) {
            return new PageResult();
        }
        //2:查分页数据
        List<Employee> rows = employeeMapper.queryForList(qo);

        return new PageResult(total, rows);
    }

    @Override
    public Employee checkName(String username) {
        return employeeMapper.checkName(username);
    }

    @Override
    public void changeState(Long id) {
        employeeMapper.changeState(id);
    }

    public Employee checkUsername(String username) {
        return employeeMapper.checkName(username);
    }

    @Override
    public void resetPassword(Long id) {
        Employee employee = employeeMapper.selectByPrimaryKey(id);
        Md5Hash md5Hash = new Md5Hash("1", employee.getUsername(), 3);
        employee.setPassword(md5Hash.toString());
        employeeMapper.resetPassword(employee);
    }

    public List<Employee> queryEmployeeListByDepartmentId(Long departmentId) {
        return employeeMapper.queryEmployeeListByDepartmentId(departmentId);
    }
//  查询用来获取班主任
    public List<Employee> queryHeadteacher() {
        return employeeMapper.queryHeadteacher();
    }

    public List<Long> queryRoleByEmpId(Long empId) {
        return employeeMapper.queryRoleByEmpId(empId);
    }
    //查找当前御用的中是否有图片
    public Employee queryPicUrlByEmpId(Long empPicId) {
        return employeeMapper.queryPicUrlByEmpId(empPicId);
    }
    //保存图片
    public void saveImageUrl(Employee employee) {
        employeeMapper.saveImageUrl(employee);
    }
}
